package org.smile.homework01.arthas.oneCase;

public class Engine {
    //1.定义构造函数
    public Engine(int id) {
        this.id = id;
        this.name = id + "号引擎";
        this.runTime = (int) (Math.random() * 10 + 1);
        this.coldTime = (int) (Math.random() * 10 + 1);

        this.coldTiming = this.coldTime;
    }

    //2.明确成员变量
    //2.1 标识一个引擎 name，id
    private final String name;
    private final int id;
    //2.2 运行时间
    private final int runTime;
    //2.3 冷却时间
    private final int coldTime;

    private int runTiming = 0;
    private int coldTiming;

    private boolean standby = true;

    public boolean isStandby() {
        return standby;
    }


    //3.明确成员函数
    //3.1 运行函数
    public void run() {
        if (this.runTiming < this.runTime) {
            this.runTiming++;
            System.out.println(this.name + "运行" + this.runTiming + "秒");
        } else {
            this.runTiming = 0;
            this.standby = false;
            System.out.println(this.name + "进入冷却");
        }

    }

    //3.2 冷却函数
    public void cold() {
        if (this.coldTiming > 0) {
            this.coldTiming--;
            System.out.println(this.name + "冷却倒计时：" + this.coldTiming + "秒");
        } else {
            this.coldTiming = this.coldTime;
            this.standby = true;
            System.out.println(this.name + "完成冷却");
        }

    }

    public boolean isAvailable(int workingEngine) {
        return this.id == workingEngine && this.standby;
    }

    public int next() {
//        if (standby) {
//            return this.id;
//        } else {
//            return this.id + 1;
//        }
        return standby ? (this.id) : (this.id + 1);  //0,1,2,3,4,5
    }

    public void describe() {
        System.out.println(this.name + "运行时间：" + this.runTime + "冷却时间：" + this.coldTime);
    }

    public void prepare() {
        if (!standby) {
            this.cold();
        }
    }


}
